//
//  UIButtonModel.swift
//  
//
//  Created by 天下宅 on 2016/11/4.
//
//

import Foundation
import UIKit

//图片 和 标题的 模式  以图片为主的 上下，左右结构
enum ButtonContentInsideModel {
    case top,left,bottom,right
}

//内容整体排版样式
enum ButtonContentModel {
    case center,left,right
}

extension UIButton
{
    func layout(withContentModel model: ButtonContentModel = .center, contentInsideModel: ButtonContentInsideModel, space:CGFloat)
    {
        let imageWidth = self.imageView?.width ?? 0
        let imageHeight = self.imageView?.height ?? 0
        
        var labelWidth:CGFloat = 0.0
        var labelHeight:CGFloat = 0.0
        
        
        var systemV:CGFloat = 0
        if let n = NumberFormatter().number(from: UIDevice.current.systemVersion)
        {
            systemV = CGFloat(n)
        }
        
        if systemV >= 8.0
        {
            labelWidth = self.titleLabel!.intrinsicContentSize.width;
            labelHeight = self.titleLabel!.intrinsicContentSize.height;
        }
        else
        {
            labelWidth = self.titleLabel!.width
            labelHeight = self.titleLabel!.height
        }
        
        var imageEdgeInsets = UIEdgeInsets.zero
        var labelEdgeInsets = UIEdgeInsets.zero
        
        
        var left:CGFloat = 10
        var right:CGFloat = self.width - (labelWidth + imageWidth + space / 2.0 + 10)
        
        switch contentInsideModel {
        case .top:
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space / 2.0, 0, 0, -labelWidth)
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageHeight-space/2.0, 0)
            right = self.width - (labelWidth > imageWidth ? labelWidth : imageWidth) - 10
        case .left:
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        case .bottom:
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWidth, 0, 0);
        case . right:
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-space/2.0, 0, imageWidth+space/2.0);
        }
        
        switch model {
        case .center:
            break;
        case .left:
            self.contentEdgeInsets = UIEdgeInsetsMake(0, left, 0, right);
        case .right:
            let temp = right;
            right = left;
            left = temp;
            //右边距固定10
            self.contentEdgeInsets = UIEdgeInsetsMake(0, left, 0, right);

        }
        
        self.titleEdgeInsets = labelEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
        
        self.titleLabel?.textAlignment = NSTextAlignment.center
    }
    
    
}

extension UIButton
{
    static func button(withTitle title:String, fontSize:CGFloat = 16, textColor:UIColor = UIColor.white, bgColor:UIColor , cornerRadius:CGFloat = 0) -> UIButton
    {
        let btn = UIButton.init(type: UIButtonType.custom)
        btn.setTitle(title, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        btn.setTitleColor(textColor, for: UIControlState.normal)
        btn.backgroundColor = bgColor
        btn.layer.cornerRadius = cornerRadius
        
        return btn
    }
}

extension UILabel
{
    static func label(withTitle title:String, fontSize:CGFloat = 16, textColor:UIColor = UIColor.white) -> UILabel
    {
        let lab = UILabel.init()
        lab.text = title
        lab.font = UIFont.systemFont(ofSize: fontSize)
        lab.textColor = textColor
        return lab
    }
}
